function Enemy(/*Vec2*/ position, /*MapManager*/ mapManager, /*EnemyFactory*/ enemy){
	this.position = position;
	this.mapManager = mapManager;
	
	this.solid = true;
	this.isEnemy = true;
	this.inMap = true;
	
	var espr = EnemySprites[enemy.spr];
	this.character = enemy;
	this.name = this.character.name;
	this.boundBox = espr.boundBox;
	this.vertices = espr.vertices;
	this.extra = espr.extra;
	
	this.viewed = false;
	this.follow = false;
	this.run = false;
	this.lost = 0;
}

Enemy.prototype.getVectorToPlayer = function(){
	var ret = null;
	var player = this.mapManager.player;
	
	if (player.position.a - this.position.a > 0){
		ret = vec2(1, 0);
	}else if (player.position.a - this.position.a < 0){
		ret = vec2(-1, 0);
	}else if (player.position.b - this.position.b > 0){
		ret = vec2(0, 1);
	}else if (player.position.b - this.position.b < 0){
		ret = vec2(0, -1);
	}
	
	return ret;
};

Enemy.prototype.moveTo = function(xTo, yTo){
	this.position.sum(xTo, yTo);
	
	if (this.mapManager.isSolid(this.position) == 1){
		this.position.sum(-xTo,-yTo);
		return false;
	}else{
		var player = this.mapManager.player;
		if (player.position.equals(this.position)){
			this.position.sum(-xTo,-yTo);			
			return false;
		}
	}
	
	return true;
};

Enemy.prototype.wander = function(){
	var x = 0, y = 0;
	x = Math.iRandom(-1,1);
	if (x == 0) y = Math.iRandom(-1,1);
	
	if (x != 0 || y != 0){
		this.moveTo(x, y);
	}
};

Enemy.prototype.castToPlayer = function(){
	var p = this.mapManager.player.position;
	
	if (p.a != this.position.a && p.b != this.position.b)
		return false;
	
	var xTo = 0, yTo = 0;
	if (p.a > this.position.a) xTo = 1; else
	if (p.a < this.position.a) xTo = -1;
	
	if (p.b > this.position.b) yTo = 1; else
	if (p.b < this.position.b) yTo = -1;
	
	if (xTo != 0 || yTo != 0){
		var ray = this.position.clone();
		var hit = false;
		var jump = vec2(xTo, yTo);
		
		while (!hit){
			ray.sum(jump);
			if (p.equals(ray)){
				this.follow = true;
				hit = true;
				return true;
			}else if (this.mapManager.isSolid(ray) == 1){
				hit = true;
				return false;
			}
		}
	}
	
	return false;
};

Enemy.prototype.isNextTo = function(p){
	var x = Math.abs(p.a - this.position.a);
	var y = Math.abs(p.b - this.position.b);
	if ((x == 1 && y == 0) || (x == 0 && y == 1)){
		return true;
	}
	
	return false;
};

Enemy.prototype.moveToPlayer = function(){
	if (this.isNextTo(this.mapManager.player.position) && !this.run){
		Console.addMessage(this.character.name + " Attacks!", "attack1", "red");
		this.mapManager.castAttack(this.character, this.mapManager.game.player);
		return;
	}
	
	var dice = Math.iRandom(10);
	if (dice == 3 || dice == 7) return;
	
	var dir = this.getVectorToPlayer();
	if (!this.moveTo(dir.a, dir.b)){
		var lp = this.mapManager.player.lastPosition;
		if (this.isNextTo(lp)){
			this.moveTo(lp.a - this.position.a, lp.b - this.position.b);
		}else{
			if (++this.lost == 5){
				this.follow = false;
				this.lost = 0;
			}
		}
	}else{
		this.lost = 0;
	}
};

Enemy.prototype.step = function(){
	if (!this.viewed || !this.follow){
		if (this.castToPlayer()){
			this.moveToPlayer();
		}else{
			this.wander();
		}
	}else{
		this.moveToPlayer();
	}
};

Enemy.prototype.draw = function(/*Context*/ ctx, /*float*/ scale){
	if (!this.viewed){
		Console.addMessage("You see a " + this.name + "!", "u1", "yellow");
		this.viewed = true;
	}
	
	var game = this.mapManager.game;
	var rh = this.mapManager.renderSizeH;
	var b = this.boundBox;
	var v = this.vertices;
	var x1, y1, x2, y2;
	
	x1 = rh.a + (b[0] * scale);
	y1 = rh.b	 + (b[1] * scale);
	x2 = b[2] * scale;
	y2 = b[3] * scale;
	
	ctx.fillStyle = "black";
	ctx.fillRect(x1,y1,x2,y2);
	
	x1 = rh.a + (v[0] * scale);
	y1 = rh.b + (v[1] * scale);
	
	for (var i=2,len=this.vertices.length;i<len;i+=2){
		x2 = rh.a + (v[i] * scale);
		y2 = rh.b + (v[i + 1] * scale);
		
		game.drawLine(x1,y1,x2,y2);
		
		x1 = x2;
		y1 = y2;
	}
	
	var dir = this.mapManager.player.direction;
	var vert = this.extra;
	
	for (var i=0,len=vert.length;i<len;i+=4){
		x1 = rh.a + (vert[i] * scale);
		y1 = rh.b + (vert[i+1] * scale);
		x2 = rh.a + (vert[i+2] * scale);
		y2 = rh.b + (vert[i+3] * scale);
		
		game.drawLine(x1,y1,x2,y2);
	}
};

Enemy.prototype.loop = function(){
	if (this.character.hp <= 0){
		this.inMap = false;
		return;
	}
	
	this.step();
};
